package com.iims.framework.db.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.iims.common.core.domain.BussinessEntity;
import com.iims.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.lang.annotation.Annotation;
import java.util.Date;

/**
 * @author fanpei
 * @date 2021/4/26
 */
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        //region 处理创建人信息
        Object createBy = this.getFieldValByName("createBy", metaObject);
        Object createTime = this.getFieldValByName("createTime", metaObject);
        if (createBy == null) {
            Annotation bussinessA = metaObject.getOriginalObject().getClass().getAnnotation(BussinessEntity.class);
            if (bussinessA != null) {
                createBy = SecurityUtils.getLoginUser().getUser().getUser_code();
            } else {
                createBy = SecurityUtils.getUsername();
            }
            this.setFieldValByName("createBy", createBy, metaObject);
        }
        if (createTime == null) {
            createTime = new Date();
            this.setFieldValByName("createTime", createTime, metaObject);
        }
        //endregion

        updateFill(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //region 处理修改人信息
        Object updateBy = this.getFieldValByName("updateBy", metaObject);
        Object updateTime = this.getFieldValByName("updateTime", metaObject);
        if (updateBy == null) {
            Annotation bussinessA = metaObject.getOriginalObject().getClass().getAnnotation(BussinessEntity.class);
            if (bussinessA != null) {
                updateBy = SecurityUtils.getLoginUser().getUser().getUser_code();
            } else {
                updateBy = SecurityUtils.getUsername();
            }
            this.setFieldValByName("updateBy", updateBy, metaObject);
        }
        if (updateTime == null) {
            updateTime = new Date();
            this.setFieldValByName("updateTime", updateTime, metaObject);
        }
        //endregion
    }

    @Override
    public boolean openInsertFill() {
        return true;
    }

    @Override
    public boolean openUpdateFill() {
        return true;
    }
}
